package com.cloudfun.campusshare.util;

import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

/**
 * @author : ashsr.zhang
 * @date : 2020/3/5 18:28
 * @Version : v1.0
 * @description
 **/
@Slf4j
public class BeanCopyUtil {

    public static <T> T copy(Object source, Class<T> targetClass,String ...ignoreProperties) {
        Class clazz = targetClass;
        if (source == null) {
            throw new IllegalArgumentException("源对象不能为空");
        }

        if (targetClass == null) {
            clazz = source.getClass();
        }
        Object obj = source;

        try {
            Object tarObj = clazz.newInstance();
            BeanUtils.copyProperties(obj, tarObj,ignoreProperties);
            return (T) tarObj;
        } catch (Exception e) {
            log.error("复制bean时异常" + e);
            throw new BusinessException(Code.ERROR);
        }
    }
}
